<?php
namespace App\Service\Admin\Mall;

class OrderService extends BaseService
{
    //商城订单

    //列表
    public static function index()
    {
        $db = self::dbConn(self::$dbMallOrder)->where('del', 0);
        $total = $db->count();
        $res = $db->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $statuss = self::$orderStatuss;
        foreach ($res as &$v) {
            $ginfo = json_decode($v->ginfo, true);
            $v->gobj = '';
            foreach ($ginfo as $gv) {
                $v->gobj .= '<img src="'.$gv['gimg'].'" style="height: 30px;"/>';
                $v->gobj .= '商品名：'.$gv['gname'];
                $v->gobj .= '，数量：'.$gv['gnum'];
                $v->gobj .= '；<br>';
            }
            $v->status_name = array_key_exists($v->status, $statuss) ? $statuss[$v->status] : '-';
        }
        return self::toback(0, '', $res, $total);
    }

    //支付
    public static function pay($id)
    {
        $res = self::dbConn(self::$dbMallOrder)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '没有订单');
        }
        if ($res->status >= 2) {
            return self::toback(-2, '状态错误');
        }
        self::dbConn(self::$dbMallOrder)
            ->where('id', $id)
            ->update(['status' => 2]);
        return self::toback();
    }

    //发货
    public static function send($id)
    {
        $res = self::dbConn(self::$dbMallOrder)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '没有订单');
        }
        if ($res->status >= 3) {
            return self::toback(-2, '状态错误');
        }
        self::dbConn(self::$dbMallOrder)
            ->where('id', $id)
            ->update(['status' => 3]);
        return self::toback();
    }

    //完成
    public static function finish($id)
    {
        $res = self::dbConn(self::$dbMallOrder)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '没有订单');
        }
        if ($res->status == 5) {
            return self::toback(-2, '状态错误');
        }
        self::dbConn(self::$dbMallOrder)
            ->where('id', $id)
            ->update(['status' => 5]);
        return self::toback();
    }
}